﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GiaoDien
{
    public partial class FORMQUYDINHSACH : Form
    {

        private DataTable bangquydinh;
        private string sqlquery;

        public FORMQUYDINHSACH()
        {
            InitializeComponent();
        }

        private void init()
        {
            bangquydinh = new DataTable();
            sqlquery = "";
        }

        private void xemtatca()
        {
            QUYDINHSACHBUS qdBUS = new QUYDINHSACHBUS();
            bangquydinh = qdBUS.Loadquydinhsach();
            this.dataGridView1.DataSource = bangquydinh;
        }

        private void FORMQUYDINHSACH_Load(object sender, EventArgs e)
        {
            xemtatca();

        }

        private void reset()
        {
            tbtenquydinh.ResetText();
            tbgiatri.ResetText();
        }

        private void lapthe()
        {
            string ten = tbtenquydinh.Text.Trim().ToUpper();
            string giatri = tbgiatri.Text.Trim().ToUpper();
            if (ten != "" && giatri != "")
            {
                QUYDINHSACHBUS qdsBUS = new QUYDINHSACHBUS();
                QUYDINHSACHDTO qdsDTO = new QUYDINHSACHDTO();
                qdsDTO.Tenquydinh = ten;
                qdsDTO.Giatri = Convert.ToInt32(giatri.ToString());

                int temp = qdsBUS.themquydinhsach(qdsDTO);
                if (temp > 0)
                {
                    MessageBox.Show("them thanh cong");
                    xemtatca();
                    reset();
                }
                else MessageBox.Show("them that bai");
            }
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {
            lapthe();
        }

        private void btnxemtatca_Click(object sender, EventArgs e)
        {
            xemtatca();
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            QUYDINHSACHBUS qdBUS = new QUYDINHSACHBUS();
            sqlquery = "SELECT * FROM QUY_DINH_SACH";
            int kq = qdBUS.update(bangquydinh, sqlquery);
            if (kq > 0)
                MessageBox.Show("Thành công");
            else
                MessageBox.Show("Thất bại");
            xemtatca();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}